<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/11/20
 * Time: 11:24 AM
 */

namespace app\admin\logic;


use app\model\TbMember;
use extend\Functions;
use think\Db;

class Member
{

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function migration()
    {
        $model      = new TbMember();
        $old_member = $model
            ->where('is_migration', 1)
            ->field('id,username,mobilephone,password,salt,nickname,happyCoin')
            ->select();
        $member     = new \app\model\Member();
        $data       = [];
        foreach ($old_member as $val) {
            array_push($data, [
                'uuid'        => strtolower(Functions::getUUID()),
                'mobile'      => $val->mobilephone,
                'username'    => $val->username,
                'nickname'    => $val->nickname,
                'password'    => $val->password,
                'encrypt'     => $val->salt,
                'balance'     => (int)($val->happyCoin * 100),
                'old_sys_uid' => $val->id,
            ]);
        }
        $result = $member
            ->isUpdate(false)
            ->saveAll($data);
        return $result;
    }
}